package com.infore.siteManagement.export.controller;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.infore.siteManagement.export.service.IExportService;

@Api(value = "导出管理接口", tags = { "导出管理接口" })
@RestController
@RequestMapping("/exports")
public class ExportController {

	@Autowired
	IExportService IExportService;
	
	@ApiOperation(value = "仪器参数信息导出")
	@ResponseBody
	@RequestMapping(value = "/equipmentExcel", method = RequestMethod.POST)
	public void exportTableData(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String file = request.getParameter("filename").toString().trim();
		String flag = request.getParameter("flag").toString();
		String filename = "";
		byte[] result = {};
		if (flag.equals("1")) {
			filename = file + ".xls";
			result = IExportService.exportExcelData(request);
		}
		try {
			// 取得文件名。
			filename = java.net.URLEncoder.encode(filename, "UTF-8");
			// 清空response
			response.reset();
			// 设置response的Header
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
			response.addHeader("Content-Length", "" + result.length);
			response.setContentType("application/vnd.ms-excel;charset=gb2312");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			toClient.write(result);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@ApiOperation(value = "站点信息导出")
	@ResponseBody
	@RequestMapping(value = "/stationExcel", method = RequestMethod.POST)
	public void exportStation(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String file = request.getParameter("filename").toString().trim();
		String flag = request.getParameter("flag").toString();
		String filename = "";
		byte[] result = {};
		if (flag.equals("1")) {
			filename = file + ".xls";
			result = IExportService.exportStation(request);
		}
		try {
			// 取得文件名。
			filename = java.net.URLEncoder.encode(filename, "UTF-8");
			// 清空response
			response.reset();
			// 设置response的Header
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
			response.addHeader("Content-Length", "" + result.length);
			response.setContentType("application/vnd.ms-excel;charset=gb2312");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			toClient.write(result);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@ApiOperation(value = "试剂信息导出")
	@ResponseBody
	@RequestMapping(value = "/reagentExcel", method = RequestMethod.POST)
	public void reagentExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String file = request.getParameter("filename").toString().trim();
		String flag = request.getParameter("flag").toString();
		String filename = "";
		byte[] result = {};
		if (flag.equals("1")) {
			filename = file + ".xls";
			result = IExportService.reagentExcel(request);
		}
		try {
			// 取得文件名。
			filename = java.net.URLEncoder.encode(filename, "UTF-8");
			// 清空response
			response.reset();
			// 设置response的Header
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
			response.addHeader("Content-Length", "" + result.length);
			response.setContentType("application/vnd.ms-excel;charset=gb2312");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			toClient.write(result);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
